package cn.z2huo.knowledge.deepunderstandjvm.classload;

import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *
 * @author z2huo
 */
class ClassLoaderTest {

    public static void main(String[] args) throws Exception {
        ClassLoader classLoader = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                String fileName = name.substring(name.lastIndexOf(",") + 1) + ".class";
                try (InputStream is = getClass().getResourceAsStream(fileName);) {
                    if (is == null) {
                        return super.loadClass(name);
                    }
                    byte[] b = new byte[is.available()];
                    is.read(b);
                    return defineClass(name, b, 0, b.length);
                } catch (IOException e) {
                    throw new ClassNotFoundException(name);
                }
            }
        };

        Object o = classLoader.loadClass("cn.z2huo.knowledge.deepunderstandjvm.classload.ClassLoaderTest")
//                .newInstance();
                .getDeclaredConstructor().newInstance();
        System.out.println(o.getClass());
        System.out.println(o instanceof ClassLoaderTest);
    }

}
